import { Internal } from '../common/Internal';
import { Column, ColumnType, IndexColumn } from './Column';

/**
 * 抽象 Table 基类，用于定义 Table 与 Column 的关系
 */
export abstract class Table<M> {
  /**
   * Table 的名称
   */
  abstract readonly tableName: string

  /**
   * 仅防止编译器认为泛型 M 没有被使用而将其移除
   */
  protected readonly nothing: M

  private readonly columns: ColumnType[] = []

  private readonly idColumns: ColumnType[] = []

  private readonly indexColumns: IndexColumn[] = []

  readonly [Internal.TableOptions]: TableOptions = {
    addColumn: function (column: ColumnType | IndexColumn) {
      if (column instanceof Column) {
        this.columns.push(column)
        if (column.isPrimaryKey) {
          this.idColumns.push(column)
        }
        return
      }
      if (column instanceof IndexColumn) {
        this.indexColumns.push(column)
        return
      }
    },
    columns: this.columns,
    idColumns: this.idColumns,
    indexColumns: this.indexColumns
  }
}

interface TableOptions {
  addColumn(column: ColumnType | IndexColumn): void

  readonly columns: readonly ColumnType[]
  readonly idColumns: readonly ColumnType[]
  readonly indexColumns: readonly IndexColumn[]
}